﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace WebUI.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString MenuActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            string attribute;
            if ((string)helper.ViewContext.RouteData.GetRequiredString("action") == actionName)
            {
                attribute = "menuitemsel";
            }
            else
            {
                attribute = "menuitem";
            }
            var htmlAttributes = new RouteValueDictionary();
            htmlAttributes.Add("class", attribute);
            return helper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(), htmlAttributes);
        }

        public static IHtmlString ToolTip(this HtmlHelper helper, String input, int length)
        {
            if (input.Length <= length)
            {
                return new MvcHtmlString(input);
            }
            else
            {
                var split = input.Split(new char[1] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                var token = split[split.Count() - 1];
                return new MvcHtmlString(String.Format("<span title=\"{2}\">{1}{0}</span>", token, "...", input));
            }
        }

        public static IHtmlString ConstructDelimitedString(this HtmlHelper helper, IEnumerable<string> list)
        {
            var result = Common.ConstructDelimitedString(list);
            return new MvcHtmlString(result);
        }

    }
}